<?php
namespace app\admin\controller;
/**
 * 短信设置
 */
use think\facade\Db;
use think\facade\View;
class Sms extends Base {
    public function index(){
        $info = Db::name('sms_set')->where('1=1')->find();
        View::assign('info',$info);
        return View::fetch();
    }
    public function save(){
        $params = input('info/a');
        $exit = Db::name('sms_set')->where('1=1')->find();

        if($exit){
            Db::name('sms_set')->where('id',$exit['id'])->update($params);
        }else{
            Db::name('sms_set')->insert($params);
        }
        return json(['status'=>1,'msg'=>'设置成功！']);
    }

    public function sendlog(){
        if(IS_AJAX){
            $page = input('page');
            $limit = input('limit')?:15;
            $map = [];
            if(input('username')){
                $map['l.username'] = input('username');
            }

            if(input('time_start') && input('time_end')){
                $time_start = strtotime(input('time_start'));
                $time_end = strtotime(input('time_end'));
                $map['l.w_time'] = ['between',[$time_start,$time_end]];
            }
            $count = Db::name('send_sms')->alias('l')->where($map)->count();

            $field = 'l.*';
            $list = Db::name('send_sms')
                ->alias('l')
                ->field($field)
                ->where($map)
                ->order('l.id desc ')
                ->page($page,$limit)
                ->select();
            foreach($list as $k=>$v){
                //$v['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
                $list[$k] = $v;
            }
            return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$list]);
        }
        $level_arr = Db::name('user_level')->field('level_name,rank')->select();
        View::assign('level_arr',$level_arr);
        return View::fetch();
    }
}